package com.examination.system.administrator.service.impl;


import com.examination.framework.exception.ExceptionCast;
import com.examination.framework.model.response.QueryResponseResult;
import com.examination.framework.model.response.QueryResult;
import com.examination.framework.model.response.ResponseResult;
import com.examination.system.administration.AreaType;
import com.examination.system.administration.response.AddAdministrationResult;
import com.examination.system.administration.response.AreaTypeCode;
import com.examination.system.administration.response.DeleteAdministrationResult;
import com.examination.system.administrator.dao.AreaTypeMapper;
import com.examination.system.administrator.dao.AreaTypeRepository;
import com.examination.system.administrator.service.AreaTypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * @author 寇淇芮
 * @Date 2019/8/27 14:16
 */
@Service
public class AreaTypeServiceImpl implements AreaTypeService {


    @Autowired
    private AreaTypeMapper areaTypeMapper;

    @Autowired
    private AreaTypeRepository areaTypeRepository;


    @Override
    @Transient
    @Transactional
    public AddAdministrationResult addAreaType(AreaType areaType) {

        //判断传入的值是否为空
        if(areaType==null){
            ExceptionCast.cast(AreaTypeCode.WITHSEVERALEXCEPTIONS);
        }
        String name = areaType.getName().trim();
        //判断是否为空字符串
        if(StringUtils.isEmpty(name)||name.equals("")){
            ExceptionCast.cast(AreaTypeCode.NAMENULLEXCEPTIONS);
        }
        //查询是否存在数据
        List<AreaType> names = areaTypeRepository.findByName(name);
        if(names == null || names.size()!=0){
            ExceptionCast.cast(AreaTypeCode.NAMEREPETITION);
        }
        //更新数据
        areaType.setName(name);
        areaType.setId(UUID.randomUUID().toString());
        return  areaTypeRepository.save(areaType) == null?new AddAdministrationResult(AreaTypeCode.FAIL,null):new AddAdministrationResult(AreaTypeCode.SUCCESS,areaType.getId());
    }

    @Override
    public QueryResponseResult findAreaType(Integer pages, Integer size) {
        int minSize = 10;
        int maxSize = 25;
        int minPage = 1;
        if (pages<minPage){
            pages = 1;
        }
        if(size<minSize || size > maxSize){
            size = 10;
        }
        PageHelper.startPage(pages,size);
        Page<AreaType> areaTypList = areaTypeMapper.findAreaTyp();
        //回写
        QueryResult queryResult = new QueryResult();
        //显示的记录
        queryResult.setList(areaTypList.getResult());
        //一共的条数
        queryResult.setTotal(areaTypList.getTotal());
        //返回数据
        return new QueryResponseResult(AreaTypeCode.SUCCESS,queryResult);
    }

    @Override
    public DeleteAdministrationResult delectAreaType(String id, AreaType areaType) {
        //判断id
        if(StringUtils.isEmpty(id)){
            ExceptionCast.cast(AreaTypeCode.IDNULLEXCEPTIONS);
        }
        if(!id.equals(areaType.getId())){
            ExceptionCast.cast(AreaTypeCode.IDNULLEXCEPTIONS);
        }
        AreaType one = this.getAreaTypeById(id);
        if(one==null){
            ExceptionCast.cast(AreaTypeCode.DATABYIDNULLEXCEPTIONS);
        }
        return areaTypeMapper.delectById(id)>0?new DeleteAdministrationResult(AreaTypeCode.SUCCESS,areaType.toString()):new DeleteAdministrationResult(AreaTypeCode.FAIL,areaType.toString());
    }

    @Override
    public ResponseResult editAreaType(String id, AreaType areaType) {
        //判断id
        if(StringUtils.isEmpty(id)){
            ExceptionCast.cast(AreaTypeCode.IDNULLEXCEPTIONS);
        }
        if(!id.equals(areaType.getId())){
            ExceptionCast.cast(AreaTypeCode.IDNULLEXCEPTIONS);
        }

        if(areaType.getName().trim().equals("")){
            ExceptionCast.cast(AreaTypeCode.NAMENULLEXCEPTIONS);
        }
        AreaType one = this.getAreaTypeById(id);
        if(one==null){
            ExceptionCast.cast(AreaTypeCode.DATABYIDNULLEXCEPTIONS);
        }

        areaType.setName(areaType.getName().trim());

        return areaTypeRepository.save(areaType) == null?new ResponseResult(AreaTypeCode.FAIL):new ResponseResult(AreaTypeCode.SUCCESS);
    }

    private AreaType getAreaTypeById(String id) {

        Optional<AreaType> optional = areaTypeRepository.findById(id);
        if(optional.isPresent()){
            return  optional.get();
        }
        return  null;

    }
}
